package com.ljy.tkmodel.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ljy.tkmodel.config.ThreadPoolConfig;
import com.ljy.tkmodel.entity.LjyZhuanshuoChapter;
import com.ljy.tkmodel.entity.LjyZhuanshuoQuestion;
import com.ljy.tkmodel.entity.vo.LjyEasyZhuanshuoChapterVO;
import com.ljy.tkmodel.mapper.LjyZhuanshuoChapterMapper;
import com.ljy.tkmodel.mapper.LjyZhuanshuoQuestionMapper;
import com.ljy.tkmodel.service.LjyZhuanshuoQuestionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ljy.tkmodel.util.RandomUtil;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * <p>
 * 专硕题库 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-01-04
 */
@Service
public class LjyZhuanshuoQuestionServiceImpl extends ServiceImpl<LjyZhuanshuoQuestionMapper, LjyZhuanshuoQuestion> implements LjyZhuanshuoQuestionService {
    @Autowired
    private LjyZhuanshuoChapterMapper chapterMapper;

    @Autowired
    private LjyZhuanshuoQuestionMapper questionMapper;
    //ljy_zhuanshuo_question  ljy_zhuanshuo_chapter
    @Cacheable(value = {"newChapters"},key = "'ChapterAll'")
    @Override
    public ArrayList<LjyZhuanshuoChapter> getTKChapterAll(HttpSession session) {
        ArrayList<LjyZhuanshuoChapter> newChapters = new ArrayList<>();
        List<LjyZhuanshuoChapter> ljyZhuanshuoChapters = chapterMapper.selectList(null);
        ArrayList<LjyZhuanshuoQuestion> questionAll = getTKQuestionAll(session);
        for (LjyZhuanshuoChapter chapter: ljyZhuanshuoChapters) {
            if (chapter.getParentId()==0){
                newChapters.add(chapter);
            }
        }
        for (LjyZhuanshuoChapter chapter1:newChapters ) {
             ArrayList<LjyZhuanshuoChapter> c1 =new ArrayList<>();
             for (LjyZhuanshuoChapter chapter2: ljyZhuanshuoChapters) {
                 if (chapter1.getChapterId().equals(chapter2.getParentId())){
                    ArrayList<LjyZhuanshuoQuestion> newQuestionList = new ArrayList<>();
                    for (LjyZhuanshuoQuestion question: questionAll) {
                         if (chapter2.getChapterId().equals(question.getChapterId()) && chapter2.getParentId().equals(question.getChapterParentId())){
                             newQuestionList.add(question);
                            }
                    }
                    chapter2.setQuestions(newQuestionList);
                    c1.add(chapter2);
                }
            }
            chapter1.setChapters(c1);
        }
        return newChapters;
    }
    @Cacheable(value = {"ljyZhuanshuoQuestions"},key = "'QuestionAll'")
    @Override
    public ArrayList<LjyZhuanshuoQuestion> getTKQuestionAll(HttpSession session) {
        ArrayList<LjyZhuanshuoQuestion> ljyZhuanshuoQuestions = (ArrayList<LjyZhuanshuoQuestion>)baseMapper.selectList(null);
        return ljyZhuanshuoQuestions;
    }

    @Override
    public ArrayList<LjyZhuanshuoChapter> getHomeAll(Integer userId) {
        return null;
    }
    @Override
    public ArrayList<LjyEasyZhuanshuoChapterVO> getEasyChapterAll() {
        ArrayList<LjyEasyZhuanshuoChapterVO> chapterMapperAll = questionMapper.getEasyChapterMapperAll();
        //学科id
        List<Integer> list1 = chapterMapperAll.stream().map(chapter -> chapter.getChapterParentId()).distinct().collect(Collectors.toList());
        //章节id
        List<Integer> list2 = chapterMapperAll.stream().map(chapter -> chapter.getChapterId()).distinct().collect(Collectors.toList());

        chapterMapperAll.stream().forEach(chapter -> list1.stream().forEach(list1Chapter -> {
            if (chapter.getChapterParentId() == list1Chapter) {
                //todo 优化查询
            }
        }));
        return  null;
    }

    @Override
    public ArrayList<LjyZhuanshuoQuestion>  getQuestionAll() {
        return questionMapper.getQuestionAll();
    }
    //只查学科章节
    @Override
    public ArrayList<LjyZhuanshuoChapter> getChapterAll() {
        ArrayList<LjyZhuanshuoChapter> newChapters = new ArrayList<>();
        QueryWrapper<LjyZhuanshuoChapter> queryWrapper = new QueryWrapper<LjyZhuanshuoChapter>();
        queryWrapper.orderByAsc("sort");
        List<LjyZhuanshuoChapter> ljyZhuanshuoChapters = chapterMapper.selectList(queryWrapper);
        for (LjyZhuanshuoChapter chapter: ljyZhuanshuoChapters) {
            if (chapter.getParentId()==0){
                newChapters.add(chapter);
                ArrayList<LjyZhuanshuoChapter> c1 =new ArrayList<>();
                for (LjyZhuanshuoChapter chapter2: ljyZhuanshuoChapters) {
                    if (chapter.getChapterId().equals(chapter2.getParentId())) {
                        c1.add(chapter2);
                    }
                }
                chapter.setChapters(c1);
            }
        }
        return newChapters;
    }
    //模拟用户选择一个章节查看题
    @Override
    public ArrayList<LjyZhuanshuoQuestion> getUserChapterTOQuestion() {
        ArrayList<LjyZhuanshuoChapter> chapterAll = getChapterAll();
        //组合全部章节
        ArrayList<LjyZhuanshuoChapter> newChapter = new ArrayList<>();
        ArrayList<LjyZhuanshuoQuestion> newQuestion = new ArrayList<>();
        //模拟用户随机取一条章节 查询题
        for (LjyZhuanshuoChapter l: chapterAll) {
            if (l.getChapters()!=null){
                for (LjyZhuanshuoChapter chapter: l.getChapters()) {
                    newChapter.add(chapter);
                }

            }
        }
        //全部章节随机出来一条
        ArrayList<LjyZhuanshuoChapter> list = (ArrayList<LjyZhuanshuoChapter>)RandomUtil.getRandom(newChapter, 1);
        Integer chapterId = list.get(0).getChapterId();
        QueryWrapper<LjyZhuanshuoQuestion> queryWrapper = new QueryWrapper<LjyZhuanshuoQuestion>();
        queryWrapper.eq("chapter_id",chapterId);
        ArrayList<LjyZhuanshuoQuestion> questions = (ArrayList<LjyZhuanshuoQuestion>)baseMapper.selectList(queryWrapper);
        return questions;
    }

    @SneakyThrows
    @Override
    @Cacheable(value = "ChapterAndQuestion")
    public ArrayList<LjyZhuanshuoChapter> AsyncQueryChapter() {
        //全部学科
        ArrayList<LjyZhuanshuoChapter> chapterParents = chapterMapper.selectChapterParent(0);
        ThreadPoolConfig config = new ThreadPoolConfig();
        ThreadPoolTaskExecutor executor = config.taskExecutor();
        //TODO 查出来的学科数量应要比核心线程数小 ， 否则就去走配置
        int size = chapterParents.size();
        ArrayList<LjyZhuanshuoChapter> parent= new ArrayList<>();
        for (LjyZhuanshuoChapter c: chapterParents) {
            CompletableFuture<LjyZhuanshuoChapter> c1 = CompletableFuture.supplyAsync(() -> {
                //全部章节
                ArrayList<LjyZhuanshuoChapter> chapters = chapterMapper.selectChapterParent( c.getChapterId());
                for (LjyZhuanshuoChapter chapter: chapters) {
                    ArrayList<LjyZhuanshuoQuestion>   questions = questionMapper.AsyncQueryQuestion(chapter.getChapterId());
                    chapter.setQuestions(questions);
                }
//                ArrayList<LjyZhuanshuoQuestion> ljyZhuanshuoQuestions = AsyncQueryQuestion(chapters);
                c.setChapters(chapters);
                return c;
            },executor);
            parent.add(c1.get());
        }
        return parent;
    }

    @Override
    public ArrayList<LjyZhuanshuoQuestion> AsyncQueryQuestion(LjyZhuanshuoChapter chapter)  {
//        ThreadPoolConfig config = new ThreadPoolConfig();
//        ThreadPoolTaskExecutor executor = config.taskExecutor();
//        CompletableFuture<ArrayList<LjyZhuanshuoQuestion>> c1 = CompletableFuture.supplyAsync(() -> {
            ArrayList<LjyZhuanshuoQuestion> questions = questionMapper.AsyncQueryQuestion(chapter.getChapterId());
            return questions;
//        },executor);
//        return c1.get();
    }

}
